package com.tansun.easycare.rule.ruleproj.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.ruleproj.dao.RuleProjectReferenceDao;
import com.tansun.easycare.rule.ruleproj.entity.RuleProject;
import com.tansun.easycare.rule.ruleproj.entity.RuleProjectReference;
import com.tansun.easycare.rule.ruleproj.entity.RuleReferenceEntity;
import com.tansun.easycare.rule.ruleproj.service.RuleProjectReferenceService;
import com.tansun.easycare.rule.ruleproj.service.RuleProjectService;

/**
 * 项目引用关系ServiceImpl
 * @author lph
 * @version 2019-09-12
 */
@Service("ruleProjectReferenceService")
@Transactional(readOnly = true)
public class RuleProjectReferenceServiceImpl extends RuleBaseServiceImpl<RuleProjectReferenceDao, RuleProjectReference> implements RuleProjectReferenceService{
	@Autowired
	private RuleProjectReferenceDao ruleProjectReferenceDao;
	@Autowired
	private RuleProjectService ruleProjectService;

	public RuleProjectReference get(String id) {
		return super.get(id);
	}
	
	public List<RuleProjectReference> findList(RuleProjectReference ruleProjectReference) {
		return super.findList(ruleProjectReference);
	}
	
	public PageInfo<RuleProjectReference> findPage(Page<RuleProjectReference> page, RuleProjectReference ruleProjectReference) {
		return super.findPage(page, ruleProjectReference);
	}
	
	@Transactional(readOnly = false)
	public void save(RuleProjectReference ruleProjectReference) {
		super.save(ruleProjectReference);
	}
	
	@Transactional(readOnly = false)
	public void delete(RuleProjectReference ruleProjectReference) {
		super.delete(ruleProjectReference);
	}

	/**
	 * 项目依赖的保存
	 * @param ruleReferenceEntity
	 * @return
	 */
	@Override
	public void insertList(RuleReferenceEntity ruleReferenceEntity) {
		String[] refProjectIds = ruleReferenceEntity.getRefProjectIds();
		//更新的话，先删除，再插入
		String refProjectCode = ruleReferenceEntity.getRefProjectCode();
		//逻辑删除，修改状态为0即可
		// ruleReferenceDao.deleteByCode(refProjectCode);
		ruleProjectReferenceDao.updateStatusByCode(refProjectCode);
		List<RuleProject> ruleProjects = ruleProjectService.getByCodeListForRef(Arrays.asList(refProjectIds));
		List<RuleProjectReference> rprList = new ArrayList<RuleProjectReference>();
		for(RuleProject ruleProject : ruleProjects){
			RuleProjectReference ruleReference = new RuleProjectReference();
			ruleReference.setProjectCode(ruleReferenceEntity.getRefProjectCode());
			ruleReference.setReferenceProjectName(ruleProject.getProjectname());
			ruleReference.setReferenceProjectCode(ruleProject.getProjectcode());
			ruleReference.setStatus("1");
			rprList.add(ruleReference);
		}
		this.insertBatch(rprList);
	}

	@Override
	public List<String> getRefProCode(String projectCode) {
		return ruleProjectReferenceDao.getRefProCode(projectCode);
	}
	
}